package com.lai.note.util;

import android.content.Context;
import android.content.Intent;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.lai.note.activity.RecordsNoteActivity;
import com.lai.note.adapter.NoteRecyclerAdapter;
import com.lai.note.dto.ResponseResult;
import com.lai.note.fragment.RecordsNoteFragment;
import com.lai.note.model.Note;
import com.lai.note.service.NoteService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;


public class ListNoteUtil {

    public static void userDeleteNote(Context context,String noteId){
        Retrofit retrofit = RetrofitRequest.getInstance(context);
        NoteService noteService = retrofit.create(NoteService.class);
        Call<ResponseResult> call = noteService.userDeleteNote(noteId);
        call.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult res = response.body();
                if(res.getStatus()==200){
                    ToastUtil.display(context,"^_^删除成功^_^");
                }else{
                    ToastUtil.display(context,"-_-删除失败-_-");
                }
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    public static void initDatas(Context context,NoteRecyclerAdapter adapter){
        List<Note> notes = new ArrayList<Note>();
        Retrofit retrofit = RetrofitRequest.getInstance(context);
        NoteService noteService = retrofit.create(NoteService.class);
        Call<ResponseResult> call = noteService.lookNoteDetails();
        call.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult res = response.body();
                System.out.println("res;--> "+res);
                if(res.getStatus()==200){
                    String noteJson = GsonUtil.object2Json(res.getData());
                    JsonParser parser = new JsonParser();
                    JsonArray jsonArray = parser.parse(noteJson.toString()).getAsJsonArray();
                    Gson gson = new Gson();
                    for(JsonElement note:jsonArray){
                        Note aNote = gson.fromJson(note, Note.class);
                        notes.add(aNote);
                    }
                    System.out.println("notes:--> "+notes);
                    adapter.refresh(notes);
                }
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    public static void userSaveNote(Context context,String noteId,String title,String newContent){
        Retrofit retrofit = RetrofitRequest.getInstance(context);
        NoteService noteService = retrofit.create(NoteService.class);
        Call<ResponseResult> call = noteService.updateNote(noteId, title, newContent);
        call.enqueue(new Callback<ResponseResult>() {
            @Override
            public void onResponse(Call<ResponseResult> call, Response<ResponseResult> response) {
                ResponseResult res = response.body();
                if(res!=null && res.getStatus()==200){
                    ToastUtil.display(context,"修改成功*_*");
                    Intent intent = new Intent(context, RecordsNoteActivity.class);
                    context.startActivity(intent);
                }
            }

            @Override
            public void onFailure(Call<ResponseResult> call, Throwable t) {

            }
        });
    }

}
